
// 通讯录Dlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "通讯录.h"
#include "ChildDlg.h"
#include "通讯录Dlg.h"
#include "afxdialogex.h"
#include "Datainterface.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// C通讯录Dlg 对话框



C通讯录Dlg::C通讯录Dlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_MY_DIALOG, pParent)
	, find(_T(""))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void C通讯录Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, List);
	DDX_Text(pDX, IDC_EDIT1, find);
}

BEGIN_MESSAGE_MAP(C通讯录Dlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, &C通讯录Dlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON4, &C通讯录Dlg::OnBnClickedButton4)
	ON_BN_CLICKED(IDC_BUTTON3, &C通讯录Dlg::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON5, &C通讯录Dlg::OnBnClickedButton5)
	ON_BN_CLICKED(IDC_BUTTON6, &C通讯录Dlg::OnBnClickedButton6)
	ON_BN_CLICKED(IDC_BUTTON7, &C通讯录Dlg::OnBnClickedButton7)
	ON_BN_CLICKED(IDC_BUTTON2, &C通讯录Dlg::OnBnClickedButton2)
END_MESSAGE_MAP()


// C通讯录Dlg 消息处理程序

BOOL C通讯录Dlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	m_static.SubclassDlgItem(IDC_STATIC, this);
	m_font.CreatePointFont(120, _T("楷体"));
	m_static.SetFont(&m_font);

	//设置ListCtrl风格
	DWORD dwStyle = List.GetExtendedStyle();
	dwStyle |= LVS_EX_FULLROWSELECT;	//选中某行使整行高亮（只适用与report风格的ListCtrl） 
	dwStyle |= LVS_EX_GRIDLINES;	    //网格线（只适用与report风格的ListCtrl） 
	List.SetExtendedStyle(dwStyle);     //设置扩展风格 

	List.InsertColumn(0, TEXT("名字"), 0, 100);
	List.InsertColumn(1, TEXT("性别"), 0, 100);
	List.InsertColumn(2, TEXT("电话"), 0, 150);
	List.InsertColumn(3, TEXT("住址"), 0, 280);
	List.InsertColumn(4, TEXT("邮箱编号"), 0, 156);
	List.InsertColumn(5, TEXT("邮箱"), 0, 156);
	List.InsertColumn(6, TEXT("QQ"), 0, 156);
	List.InsertColumn(7, TEXT("备注"), 0, 200);

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void C通讯录Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void C通讯录Dlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR C通讯录Dlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void C通讯录Dlg::UpdataList() {  //更新V链表List函数
	List.DeleteAllItems();
	CString str;
		for (int i = 0; i < datainterface.v_info.size(); i++) {
			str.Format(TEXT("%s"), datainterface.v_info[i].m_name.c_str());
			List.InsertItem(i, str);
			str.Format(TEXT("%s"), datainterface.v_info[i].m_sex.c_str());
			List.SetItemText(i, 1, str);
			str.Format(TEXT("%s"), datainterface.v_info[i].m_tele.c_str());
			List.SetItemText(i, 2, str);
			str.Format(TEXT("%s"), datainterface.v_info[i].m_address.c_str());
			List.SetItemText(i, 3, str);
			str.Format(TEXT("%s"), datainterface.v_info[i].m_mailcode.c_str());
			List.SetItemText(i, 4, str);
			str.Format(TEXT("%s"), datainterface.v_info[i].m_mail.c_str());
			List.SetItemText(i, 5, str);
			str.Format(TEXT("%s"), datainterface.v_info[i].m_QQ.c_str());
			List.SetItemText(i, 6, str);
			str.Format(TEXT("%s"), datainterface.v_info[i].m_type.c_str());
			List.SetItemText(i, 7, str);
		}
	/*else {
		for (int i = 0; i < datainterface.f_info.size(); i++) {
			str.Format(TEXT("%s"), datainterface.f_info[i].m_name.c_str());
			List.InsertItem(i, str);
			str.Format(TEXT("%s"), datainterface.f_info[i].m_sex.c_str());
			List.SetItemText(i, 1, str);
			str.Format(TEXT("%s"), datainterface.f_info[i].m_tele.c_str());
			List.SetItemText(i, 2, str);
			str.Format(TEXT("%s"), datainterface.f_info[i].m_address.c_str());
			List.SetItemText(i, 3, str);
			str.Format(TEXT("%s"), datainterface.f_info[i].m_mailcode.c_str());
			List.SetItemText(i, 4, str);
			str.Format(TEXT("%s"), datainterface.f_info[i].m_mail.c_str());
			List.SetItemText(i, 5, str);
			str.Format(TEXT("%s"), datainterface.f_info[i].m_QQ.c_str());
			List.SetItemText(i, 6, str);
			str.Format(TEXT("%s"), datainterface.f_info[i].m_type.c_str());
			List.SetItemText(i, 7, str);
		}*/
	}



//各个按钮的定义
void C通讯录Dlg::OnBnClickedButton1() {  //打开文件
	// TODO: 在此添加控件通知处理程序代码
	// 设置过滤器   
	TCHAR szFilter[] = _T("文本文件(*.txt)|*.txt|所有文件(*.*)|*.*||");
	// 构造打开文件对话框   
	CFileDialog fileDlg(TRUE, _T("txt"), NULL, 0, szFilter, this);
	// 显示打开文件对话框   
	if (IDOK == fileDlg.DoModal())
	{
		// 如果点击了文件对话框上的“打开”按钮，则将选择的文件路径显示到编辑框里   
		strFilePath = fileDlg.GetPathName();
        datainterface.Open(strFilePath);
		UpdataList();
	}

}

void C通讯录Dlg::OnBnClickedButton2() //保存文件
{
	// TODO: 在此添加控件通知处理程序代码
	if (datainterface.Save(strFilePath))
		MessageBox(TEXT("已保存"), TEXT("提示"));
	else MessageBox(TEXT("保存失败"), TEXT("提示"));

}

void C通讯录Dlg::OnBnClickedButton3() //退出程序
{
	// TODO: 在此添加控件通知处理程序代码
	exit(0);
}

void C通讯录Dlg::OnBnClickedButton4() //新建联系人
{
	// TODO: 在此添加控件通知处理程序代码
	ChildDlg child;
	if (IDOK == child.DoModal()) {
		Info info(child.m_name.GetBuffer(), child.m_sex.GetBuffer(), child.m_tele.GetBuffer(), child.m_address.GetBuffer(), child.m_mailcode.GetBuffer(), child.m_mail.GetBuffer(), child.m_QQ.GetBuffer(), child.m_type.GetBuffer()); //转化格式
		datainterface.Add(info);
		UpdataList();
	}
}


void C通讯录Dlg::OnBnClickedButton5() //删除联系人
{
	// TODO: 在此添加控件通知处理程序代码
	int lect = List.GetSelectionMark();
	if (lect > -1) {
		UINT i = MessageBox(_T("确定要删除此联系人"), _T("提示"), MB_YESNO | MB_ICONQUESTION);
		if (i == IDYES) {
			datainterface.Del(lect);
			MessageBox(_T("删除成功！"), TEXT("提示"));
			UpdataList();
		}
		else {
			return;
		}
	}
	else {
		MessageBox(_T("请先点击通讯录要删除的联系人"), TEXT("提示"));
	}
}


void C通讯录Dlg::OnBnClickedButton6() //修改联系人
{
	// TODO: 在此添加控件通知处理程序代码
	int lect = List.GetSelectionMark();
	if (lect > -1) {
		CString str;
		ChildDlg child;
		str.Format(TEXT("%s"), datainterface.v_info[lect].m_name.c_str());
		child.m_name = str;
		str.Format(TEXT("%s"), datainterface.v_info[lect].m_sex.c_str());
		child.m_sex = str;
		str.Format(TEXT("%s"), datainterface.v_info[lect].m_tele.c_str());
		child.m_tele = str;
		str.Format(TEXT("%s"), datainterface.v_info[lect].m_address.c_str());
		child.m_address = str;
		str.Format(TEXT("%s"), datainterface.v_info[lect].m_mailcode.c_str());
		child.m_mailcode = str;
		str.Format(TEXT("%s"), datainterface.v_info[lect].m_mail.c_str());
		child.m_mail = str;
		str.Format(TEXT("%s"), datainterface.v_info[lect].m_QQ.c_str());
		child.m_QQ = str;
		str.Format(TEXT("%s"), datainterface.v_info[lect].m_type.c_str());
		child.m_type = str;
		if (IDOK == child.DoModal()) {
			Info info(child.m_name.GetBuffer(), child.m_sex.GetBuffer(), child.m_tele.GetBuffer(), child.m_address.GetBuffer(), child.m_mailcode.GetBuffer(), child.m_mail.GetBuffer(), child.m_QQ.GetBuffer(), child.m_type.GetBuffer()); //转化格式
			datainterface.Amend(lect, info);
			}
			MessageBox(_T("修改成功！"), TEXT("提示"));
			UpdataList();
		}
	    else {
		    MessageBox(_T("请先点击通讯录中要修改的联系人"), TEXT("提示"));  
	}
}


void C通讯录Dlg::OnBnClickedButton7() //查找函数的定义
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	Info FindInfo = datainterface.Find(find.GetBuffer());
	if (FindInfo.m_name == "0") {
		MessageBox(TEXT("查无此人！"), TEXT("提示"));
	}
	else {
		CString str;
		List.DeleteAllItems();
		for (int i = 0; i < datainterface.f_info.size(); i++) {
			str.Format(TEXT("%s"), datainterface.f_info[i].m_name.c_str());
			List.InsertItem(i, str);
			str.Format(TEXT("%s"), datainterface.f_info[i].m_sex.c_str());
			List.SetItemText(i, 1, str);
			str.Format(TEXT("%s"), datainterface.f_info[i].m_tele.c_str());
			List.SetItemText(i, 2, str);
			str.Format(TEXT("%s"), datainterface.f_info[i].m_address.c_str());
			List.SetItemText(i, 3, str);
			str.Format(TEXT("%s"), datainterface.f_info[i].m_mailcode.c_str());
			List.SetItemText(i, 4, str);
			str.Format(TEXT("%s"), datainterface.f_info[i].m_mail.c_str());
			List.SetItemText(i, 5, str);
			str.Format(TEXT("%s"), datainterface.f_info[i].m_QQ.c_str());
			List.SetItemText(i, 6, str);
			str.Format(TEXT("%s"), datainterface.f_info[i].m_type.c_str());
			List.SetItemText(i, 7, str);
		}
	}
}



